package com.ruoyi.rescue.controller;


import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rescue.api.ReShopNameService;
import com.ruoyi.rescue.api.domain.CheShop;
import com.ruoyi.rescue.domain.CheGroupBuying;
import com.ruoyi.rescue.service.ICheGroupBuyingService;
import com.ruoyi.shop.api.CheApiShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 优惠团购Controller
 * 
 * @author ruoyi
 * @date 2024-06-05
 */
@RestController
@CrossOrigin
@RequestMapping("/buying")
public class CheGroupBuyingController extends  BaseController
{
    @Autowired
    private ICheGroupBuyingService cheGroupBuyingService;

    @Autowired
    private ReShopNameService reShopNameService;

    /**
     * 查询优惠团购列表
     */
//    @RequiresPermissions("buying:buying:list")
    @GetMapping("/list")
    public TableDataInfo list(CheGroupBuying cheGroupBuying)
    {
        startPage();
        List<CheGroupBuying> list = cheGroupBuyingService.selectCheGroupBuyingList(cheGroupBuying);

        //获取商家名
        for(CheGroupBuying buying : list){
//            System.out.println(buying.getShopId());
            AjaxResult info = reShopNameService.getInfo(buying.getShopId(), SecurityConstants.INNER);
            LinkedHashMap  data = (LinkedHashMap) info.get("data");
            CheShop cheShop = BeanUtil.mapToBean(data, CheShop.class, true);
            System.out.println(cheShop.getShopName());
            buying.setShopName(cheShop.getShopName());
        }

        return getDataTable(list);
    }

    /**
     * 导出优惠团购列表
     */
    @RequiresPermissions("buying:buying:export")
    @Log(title = "优惠团购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheGroupBuying cheGroupBuying)
    {
        List<CheGroupBuying> list = cheGroupBuyingService.selectCheGroupBuyingList(cheGroupBuying);
        ExcelUtil<CheGroupBuying> util = new ExcelUtil<CheGroupBuying>(CheGroupBuying.class);
        util.exportExcel(response, list, "优惠团购数据");
    }

    /**
     * 获取优惠团购详细信息
     */
    //---------------------
//    @RequiresPermissions("buying:buying:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheGroupBuyingService.selectCheGroupBuyingById(id));
    }

    /**
     * 新增优惠团购
     */
    @RequiresPermissions("buying:buying:add")
    @Log(title = "优惠团购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheGroupBuying cheGroupBuying)
    {
        return toAjax(cheGroupBuyingService.insertCheGroupBuying(cheGroupBuying));
    }

    /**
     * 修改优惠团购
     */
    @RequiresPermissions("buying:buying:edit")
    @Log(title = "优惠团购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheGroupBuying cheGroupBuying)
    {
        return toAjax(cheGroupBuyingService.updateCheGroupBuying(cheGroupBuying));
    }

    /**
     * 删除优惠团购
     */
    @RequiresPermissions("buying:buying:remove")
    @Log(title = "优惠团购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheGroupBuyingService.deleteCheGroupBuyingByIds(ids));
    }
}
